<?php
ini_set('date.timezone','Asia/Shanghai');//调整时区
session_start();
$robot=$_SESSION['robot'];
$s_time=$_SESSION['s_time'];
$e_time=$_SESSION['e_time'];
$time=$_SESSION['time'];
$table=$_SESSION['table'];
//$robot1="ap4";
//$robot2="ap4";
//$s_time="2017-04-07 08:00";
//$e_time="2017-04-07 12:00";
//$time=10;
$data=array();
if($time==1){
    $f=floor((strtotime($e_time)-strtotime($s_time))/86400);
    $f=floor((strtotime($e_time)-strtotime($s_time))%86400/3600)+$f*24;
    $temp=$s_time;
    for($i=0;$i<=$f;$i++){
        $date0[$temp] =$temp;
        $value0[$temp] =0;
        $temp=date('Y-m-d H:i',strtotime("$temp+1hours"));
    }
    $num=13;
}else if($time==10){
    //  $f=floor((strtotime($e_time)-strtotime($s_time))/86400);
    //  $f=floor((strtotime($e_time)-strtotime($s_time))%86400/3600)+$f*24;
    $f=floor((strtotime($e_time)-strtotime($s_time))%86400/600);
    //echo $f."<br/>";
    $temp=$s_time;
    for($i=0;$i<=$f;$i++){
        $date0[$temp] =$temp;
        $value0[$temp] =0;
        $temp=date('Y-m-d H:i',strtotime("$temp+10minute"));
    }
    $num=15;
}
/*连接数据库*/
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "data";

$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
$array = explode(",",$robot);
$robot_where="hdid = '$array[0]'";
for($index=1;$index<count($array);$index++)
{
    $robot_where=$robot_where."or hdid = '$array[$index]'";
}
//$sql="SELECT * , COUNT( * ) AS count, SUBSTRING(  `date` , 1, 15 ) AS dateindex FROM msg WHERE mtype =10000 and (hdid='$hdid1'or hdid ='$hdid2')  GROUP BY user_id HAVING  SUBSTRING(  `date` , 1, '$num' )='$time' order by user_name asc";
$sql="SELECT * , COUNT( * ) AS count, SUBSTRING(  `date` , 1, '$num' ) AS dateindex FROM msg WHERE mtype =10000 and (".$robot_where.") and( date BETWEEN '$s_time'and '$e_time') GROUP BY user_id order by user_id asc";

//echo $sql."a<br/>";
$result = $conn->query($sql);
$group=array();
if ($result->num_rows > 0) {
    // 输出每行数据
    while($row = $result->fetch_assoc()) {
        if($row['count']>10){
            $name=$row['user_id'];
           // echo "Name:".$name."<br/>";
           // $sql1="SELECT * , COUNT( * ) AS count, SUBSTRING(  `date` , 1, 15 ) AS dateindex FROM msg WHERE mtype =10000 and (hdid='$hdid1'or hdid ='$hdid2') AND  SUBSTRING(  `date` , 1, '$num' )='$time'   GROUP BY SUBSTRING(  `date` , 1, 15 )  having user_name='$name'";
            $sql1="SELECT * , COUNT( * ) AS count, SUBSTRING(  `date` , 1, '$num' ) AS dateindex FROM msg WHERE user_id='$name' and mtype =10000 and (".$robot_where.") and( date BETWEEN '$s_time'and '$e_time') GROUP BY SUBSTRING(  `date` , 1, '$num' ) ";

             //echo $sql1.'<br/>';
           // $date=$date0;
            $value=$value0;
            $res = $conn->query($sql1);
            if ($res->num_rows > 0) {
                // 当前组第一条数据
                while($row = $res->fetch_assoc()) {
                    if($time==1)
                        $temp=date("Y-m-d H:i",strtotime( $row['dateindex'].":00:00"));
                    else if($time==10)
                        $temp=date("Y-m-d H:i",strtotime( $row['dateindex']."0:00"));
                    // $data['count']=$row['count'];//按10分钟统计得到的值
            //        $date[$temp] =$temp;
                    $value[$temp] =$row['count'];
                }
            }
           // $tt = date('H:i',strtotime("$_time+10minute"));
           // $group_time_ary['date'] = $date;
            $group_time_ary['value'] = $value;
            $group[$name]=$group_time_ary;
        }
    }
} else{
    echo "没有数据";
}
$ret_data = array();
$ret_data['res'] = $group;
echo json_encode($ret_data);
$conn->close();
?>